package com.bilin.coin.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.bean.CoinBean;
import com.bilin.coin.bean.ExchangeParamBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.MoneyUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = "/auction/ExchangeUsdtToMgpActivity")
public class ExchangeUsdtToMgpActivity extends BaseAppCompatActivity {

    @BindView(R.id.tv_coin_name)
    TextView tv_coin_name;
    @BindView(R.id.et_amount)
    EditText et_amount;
    @BindView(R.id.tv_usable)
    TextView tv_usable;
    @BindView(R.id.tv_mgp_num)
    TextView tv_mgp_num;
    @BindView(R.id.tv_wgp_num)
    TextView tv_wgp_num;

    @Autowired
    CoinBean coinBean;
    private ExchangeParamBean exchangeParamBean;

    @Override
    protected void initView() {
        setTitle(""+coinBean.getTitle()+"");
        initEvent();
        getData();
    }

    private void initEvent() {
        et_amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String amount = s.toString().trim();
                try {
                    double parseDouble = Double.parseDouble(amount);
                    double mgp = parseDouble*exchangeParamBean.getMgp()/exchangeParamBean.getUsdt();
                    //double wgp = parseDouble*exchangeParamBean.getWgp()*0.5;

                    tv_mgp_num.setText(MoneyUtils.decimal2ByUp(new BigDecimal(mgp+"")).toPlainString());
                    //tv_wgp_num.setText(MoneyUtils.decimal2ByUp(new BigDecimal(wgp+"")).toPlainString());
                } catch (Exception e) {
                    //et_amount.setText("0");
                    tv_mgp_num.setText("0.00");
                    //tv_wgp_num.setText("0.00");
                }
            }
        });
    }

    private void getData() {
        new Geter(this, true, false) {
            @Override
            protected String fillUrl() {
                return "/api/wallet/getPriceMgp";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                exchangeParamBean = GsonUtil.GsonToBean(dataJson, ExchangeParamBean.class);
                tv_usable.setText("可用MGP："+ MoneyUtils.decimal2ByUp(new BigDecimal(exchangeParamBean.getMgpUsable()+"")));
            }
        };
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_exchange_mgp_to_usdt;
    }

    @OnClick({R.id.bt_all, R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_all:
                if (exchangeParamBean == null) return;
                et_amount.setText(exchangeParamBean.getMgpUsable()+"");
                break;
            case R.id.bt_confirm:
                confirm();
                break;
        }
    }

    private void confirm() {
        new Poster(this, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/wallet/transferMapToU";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("amount",et_amount.getText().toString());
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                finish();
            }
        };
    }
}
